<template>
	<!-- 简历附件 -->
	<view class="">
		<!-- 头部 -->
		<view class=" text-bold" style="font-size: 40upx;padding: 40upx 30upx;">
			上传简历照片
		</view>
		<view class="mb10 plr15" style="position: relative;" v-for='(item,index) in imgList' :key='index'>
			<image style="width: 100%;height: 403upx;" :src="item.path||'/static/images/logo.png'" mode="aspectFill" @tap.stop='yulan(item,index)'></image>
			<view class="cu-tag bg-red" @tap.stop="DelImg(item,index)" style="z-index: 100;"><text class="cuIcon-close"></text></view>
		</view>
		<view class="card_item" @tap='chooseImg()' v-if="imgList==''||imgList.length<9">
			<view class=" margin-tb-sm">
				<view class="">
					<image class="bg_img" :src="IMG_URL+'static/miniwechat/cardbg.png'" mode="aspectFill"></image>
				</view>
				<view class="btn_chuan ">
					<view class="btn_center flex align-center justify-center">
						<image :src="IMG_URL+'static/miniwechat/xiangji@2x.png'" mode="aspectFill"></image>
						<view class="" style="">上传简历照片</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 底部按钮 -->
		<view class="btn_next bg_deep " style="margin-bottom: 20upx;" @tap="sub">确定</view>
	</view>
</template>

<script>
	let that;
	export default {
		data() {
			return {
				imgList: [], //简历照片
				maxNum: 9,
				imgList_path: [], //
				IMG_URL:this.IMG_URL,
			};
		},
		onLoad() {
			this.user = uni.getStorageSync('USERINFO');
			uni.showLoading({})
			this.getInfo()
			this.imgList_path = []
		},
		methods: {
			getInfo() {
				this.$Request.post(this.$api.company.upload_resume, {
					type: 0 //获取简历
				}).then(res => {
					if (res.code == '1') {
						uni.hideLoading()
						if (res.data == '') {
							this.imgList = []
							return
						}
						this.imgList = res.data.resume_id_url
						this.imgList.map(item => {
							this.imgList_path.push(item.path);
						});
						// this.imgList_id=res.data.resume_id
					}
				});
			},
			chooseImg() {
				var _this = this
				// #ifdef APP-PLUS
				plus.android.requestPermissions(['android.permission.WRITE_EXTERNAL_STORAGE'], function(e) {
				
					var result = 0;
					for (var i = 0; i < e.granted.length; i++) {
						var grantedPermission = e.granted[i];
						console.log('已获取的权限：' + grantedPermission);
						result = 1
					}
					for (var i = 0; i < e.deniedPresent.length; i++) {
						var deniedPresentPermission = e.deniedPresent[i];
						console.log('拒绝本次申请的权限：' + deniedPresentPermission);
						result = 0
					}
					for (var i = 0; i < e.deniedAlways.length; i++) {
						var deniedAlwaysPermission = e.deniedAlways[i];
						console.log('永久拒绝申请的权限：' + deniedAlwaysPermission);
						result = -1
					}
					console.log(result);
					if(result === 1){
						_this.$Request.uploadImg((res) => {
							_this.$Common.toast(res.msg);
							if (res.code == 1) {
								let pic = []
								pic = res.data;
								_this.imgList_path = []
								_this.imgList = _this.imgList.concat(pic)
								_this.imgList.map(item => {
									_this.imgList_path.push(item.path);
								});
								console.log(_this.imgList, '图片');
						
							}
						}, _this.maxNum)
					}else if (result === 0){
						uni.showModal({
							title: '亲爱的用户',
							content: '您需要打开相册权限，才能使用完整功能。若您需要，请点击同意，重新授权',
							success(res) {
								if (res.confirm) {
									plus.android.requestPermissions(['android.permission.WRITE_EXTERNAL_STORAGE'])
								}
							}
						});
					}else{
						uni.showModal({
							title: '提示',
							content: '相册授权已被拒绝，请前往设置打开应用相册权限',
							showCancel: false,
							success: function (res) {
								if (res.confirm) {
									var main = plus.android.runtimeMainActivity();  
									var Intent = plus.android.importClass("android.content.Intent");
									var mIntent = new Intent('android.settings.WIRELESS_SETTINGS');  
									main.startActivity(mIntent); 
								}
							}
						});
					}
				}, function(e) {
					console.log('Request Permissions error:' + JSON.stringify(e));
				});
				// #endif
				// #ifdef MP
				_this.$Request.uploadImg((res) => {
					_this.$Common.toast(res.msg);
					if (res.code == 1) {
						let pic = []
						pic = res.data;
						_this.imgList_path = []
						_this.imgList = _this.imgList.concat(pic)
						_this.imgList.map(item => {
							_this.imgList_path.push(item.path);
						});
						console.log(_this.imgList, '图片');
				
					}
				}, _this.maxNum)
				// #endif
			},
			yulan(pic, index) { //预览
				this.$Common.lookImg(this.imgList_path, index);
			},
			DelImg(item, index) {
				//删除视频
				uni.showModal({
					content: '确定要删除吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(index, 1);
						}
					}
				});

			},
			sub() { //提交
				if (this.imgList == '') {
					this.$Common.toast('请先上传简历照片');
					return false;
				}
				var skill_certificate = [];
				this.imgList &&
					this.imgList.map(item => {
						skill_certificate.push(item.id);
					});
				this.$Request.post(this.$api.company.upload_resume, {
					type: 1, //上传
					resume_id: skill_certificate,
				}).then(res => {
					this.$Common.toast(res.msg)
					if (res.code == '1') {
						uni.$emit('authen', {})
						setTimeout(() => {
							uni.navigateBack({})
						}, 600)
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.card_item {
		width: 686upx;
		height: 403upx;
		margin: auto;
		position: relative;
		margin-bottom: 40upx;

		.bg_img {
			width: 686upx;
			height: 403upx;
		}
	}

	.cu-tag {
		position: absolute;
		right: 30upx;
		top: 0;
		z-index: 10;
	}

	.btn_chuan {
		position: absolute;
		bottom: 0;
		top: 0;
		left: 0;
		right: 0;
		margin: auto;
		width: 370upx;
		height: 77upx;
		background-color: #1B90FF;
		color: #FFFFFF;
		font-size: 28upx;
		border-radius: 39upx;
		text-align: center;
		z-index: 10;

		.btn_center {
			margin: auto;
			width: 370upx;
			padding: 20upx 0;
			line-height: 1.2;
			text-align: center;

			image {
				width: 34upx;
				margin-right: 17upx;
				height: 26upx;
			}
		}

	}
</style>
